from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec  # 期待元素条件
from selenium.webdriver.common.action_chains import ActionChains
import datetime, time, os


class BasePage:

    def __init__(self, driver: WebDriver):
        self.driver = driver  # 初始化driver对象
        self.action = ActionChains(self.driver)

    # 元素等待--显性等待
    def wait_ele_visible(self, loc, timeout=20, poll=0.5):
        """
        :param loc:   元素定位
        :param name:   My_log()  名字  ----  页面名字
        :param mes:    My_log().info()  名字 ----  元素名字
        :param timeout:
        :param poll:
        :return:
        """
        try:
            ele = WebDriverWait(driver=self.driver, timeout=timeout, poll_frequency=poll).until(
                lambda x: self.driver.find_element(*loc))  # 可见元素
        except Exception as e:
           print(e)
        else:
            return ele

    # 页面元素是否存在
    def wait_page_contain_element(self, loc, timeout=20, poll=0.5):
        try:
            self.wait_ele_visible(loc=loc, timeout=timeout, poll=poll)
        except Exception as e:
            print(e)


    # 点击
    def click_element(self, loc):
        try:
            ele = self.wait_ele_visible(loc=loc, timeout=3, poll=0.5,)
            ele.click()
        except Exception as e:
            print(e)



    # 输入
    def input_text(self, text, loc):
        try:
            ele = self.wait_ele_visible(loc=loc, timeout=20, poll=0.5,)
            ele.send_keys(text)
        except:
            print()


    # 查看属性内容
    def get_ele_attribute(self, loc, g_att, timeout=5, poll=0.5):
        try:
            ele = self.wait_ele_visible(loc=loc, timeout=timeout, poll=poll)
            value = ele.get_attribute(g_att)
        except Exception as e:
            print()
        else:
            return value

    # 查看文本内容
    def get_ele_text(self, loc, timeout=20, poll=0.5):
        try:
            ele = self.wait_ele_visible(loc=loc, timeout=timeout, poll=poll)
            value = ele.text
        except Exception as e:
           print()
        else:
            return value



    # 上下滑动
    def execute_js(self, dis):
        """
        :param dis:  上下滚动的距离
        """
        try:
            js = 'window.scrollTo(0,{})'.format(dis)  #  滚动。。距离
            # js = 'document.getElementById({}).scrollTo({top:{}})'.format(loc,dis)  # 滚动到。。。位置
            # js = 'var q=document.documentElement.scrollTop({})'.format(dis)
            self.driver.execute_script(js)
            time.sleep(0.5)
        except Exception as e:
            print()
    
      # 上下滑动
    def execute_js_return(self, js):
     
        try:
            ret = self.driver.execute_script(js)
            print(ret)
            time.sleep(0.5)
            return ret
        except Exception as e:
            print(e);
            print('execute_js  is error')

    # 鼠标悬浮
    def action_suspend(self, loc,):
        try:
            ele = self.wait_ele_visible(loc=loc, )
            self.action.move_to_element(ele).perform()
            time.sleep(1)
        except Exception as e:
           print(e)

    def action_click(self,loc):
        try:
            ele = self.wait_ele_visible(loc=loc)
            self.action.click(ele)
            time.sleep(1)
        except Exception as e:
            print(e)

